import { getWbAuthInfo, authToUserWb, commitWbToken, getWbList, getWbMenus, setWbMenus, getWbSubscribeReply, setWbSubscribeReply,
         setWbSubReplyStatus, getWbDefaultReply, setWbDefaultReply, setWbDefReplyStatus, getWbReplyTail, setWbReplyTail,
         setWbReplyTailStatus, getWbKwReply, setWbKwReply, delWbKwReply, getWbFansList, getWbGroupList, createWbGroup,
         delWbGroup, setWbFansGroup, getMyWbList, getWbEmotions, sendWeibo, wbFansSync } from '@/service/getData'
import * as type from '@/store/mutation-types'
const state = {
  wbList: [], // 微博列表
  authInfo: {}, // 授权信息
  weiboMenu: [], // 菜单
  subReply: { // 关注回复
    type: 1,
    message: '',
    status: 1
  },
  defaultReply: {}, // 私信自动回复
  kwReply: { // 关键词回复
    list: [],
    page: {}
  },
  tail: {}, // 小尾巴
  fansList: { // 粉丝列表
    list: [],
    page: {}
  },
  fansGroup: [], // 粉丝群组
  myWeibo: { // 我的微博
    list: [],
    page: {}
  },
  emojis: [] // 表情
}
const getters = {
  classifyEmoji: state => {
    const emoji = {}
    state.emojis.forEach(item => {
      item.category = item.category || '默认'
      emoji[item.category] = emoji[item.category] || []
      emoji[item.category].push(item)
    })
    return emoji
  }
}
const actions = {
  async getWbAuthInfo ({commit, state}) {
    const res = await getWbAuthInfo()
    commit(type.SET_WB_AUTH_INFO, res.data.data)
    return res
  },
  async commitWbToken ({commit, state}, payload) {
    const res = await commitWbToken(payload)
    return res
  },
  async authToUser ({commit, state}, payload) {
    const res = await authToUserWb(payload)
    return res
  },
  async getWbList ({commit, state}, payload) {
    const res = await getWbList(payload)
    commit(type.SET_WB_LIST, res.data.data)
    return res
  },
  async sendWeibo ({commit, state}, payload) {
    const res = await sendWeibo(payload)
    return res
  },
  // 自定义菜单
  async getWbMenus ({commit, state}, appid) {
    const res = await getWbMenus(appid)
    const buttons = res.data.data.button.map(item => {
      if (!item.sub_button) {
        item.sub_button = []
      }
      return item
    })
    commit(type.SET_WB_MENU, buttons)
  },
  async setWbMenus ({commit, state}, payload) {
    const res = await setWbMenus(payload)
    return res
  },
  // 自动回复
  async getWbSubscribeReply ({commit, state}, appid) {
    const res = await getWbSubscribeReply(appid)
    commit(type.SET_WB_SUB_REPLY, res.data.data)
    return res
  },
  async setWbSubscribeReply ({commit, state}, payload) {
    const res = await setWbSubscribeReply(payload)
    return res
  },
  async setWbSubReplyStatus ({commit, state}, payload) {
    const res = await setWbSubReplyStatus(payload)
    return res
  },
  async getDefaultReply ({commit, state}, appid) {
    const res = await getWbDefaultReply(appid)
    commit(type.SET_WB_DEFAULT_REPLY, {...res.data.data, end: res.data.data.endTime, start: res.data.data.startTime})
    return res
  },
  async setDefaultReply ({commit, state}, payload) {
    const res = await setWbDefaultReply(payload)
    return res
  },
  async setDefReplyStatus ({commit, state}, payload) {
    const res = await setWbDefReplyStatus(payload)
    return res
  },
  async getReplyTail ({commit, state}, appid) {
    const res = await getWbReplyTail(appid)
    commit(type.SET_WB_REP_TAIL, res.data.data)
    return res
  },
  async setReplyTail ({commit, state}, payload) {
    const res = await setWbReplyTail(payload)
    return res
  },
  async setReplyTailStatus ({commit, state}, payload) {
    const res = await setWbReplyTailStatus(payload)
    return res
  },
  async getKwReply ({commit, state}, payload) {
    const res = await getWbKwReply(payload)
    commit(type.SET_WB_KW_REPLY, res.data.data)
    return res
  },
  async setKwReply ({commit, state}, payload) {
    const res = await setWbKwReply(payload)
    return res
  },
  async delKwReply ({commit, state}, payload) {
    const res = await delWbKwReply(payload)
    return res
  },
  // 粉丝管理
  async getFansList ({commit, state}, payload) {
    const res = await getWbFansList(payload)
    commit('SET_FANS_LIST', res.data.data)
    for (let i = 0, length = state.fansList.list.length; i < length; i++) {
      state.fansList.list[i].follow
    }
  },
  async getGroupList ({commit, state}, payload) {
    const res = await getWbGroupList(payload)
    commit('SET_FANS_GROUP', res.data.data)
    return res
  },
  async createGroup ({commit, state}, payload) {
    const res = await createWbGroup(payload)
    return res
  },
  async setWbFansGroup ({commit, state}, payload) {
    const res = await setWbFansGroup(payload)
    return res
  },
  async delWbGroup ({commit, state}, payload) {
    const res = await delWbGroup(payload)
    return res
  },
  async wbFansSync ({commit, state}, appid) {
    const res = await wbFansSync(appid)
    return res
  },
  // 我的微博
  async getMyWbList ({commit, state}, payload) {
    const res = await getMyWbList(payload)
    commit('SET_MY_WEIBO', res.data.data)
    return res
  },
  async getEmotions ({commit, state}, appid) {
    const res = await getWbEmotions(appid)
    commit('SET_EMOJIS', res.data.data)
    return res
  }
}
const mutations = {
  [type.SET_WB_AUTH_INFO] (state, payload) {
    state.authInfo = payload
  },
  [type.SET_WB_LIST] (state, payload) {
    state.wbList = payload
  },
  [type.SET_WB_MENU] (state, payload) {
    state.weiboMenu = payload
  },
  [type.SET_WB_SUB_REPLY] (state, payload) {
    if (payload) {
      state.subReply = payload
    }
  },
  [type.SET_WB_DEFAULT_REPLY] (state, payload) {
    state.defaultReply = payload
    state.defaultReply.startTime = payload.startTime.slice(0, -3)
    state.defaultReply.endTime = payload.endTime.slice(0, -3)
  },
  [type.SET_WB_KW_REPLY] (state, payload) {
    state.kwReply = payload
  },
  [type.SET_WB_REP_TAIL] (state, payload) {
    state.tail = payload
  },
  ['SET_FANS_LIST'] (state, payload) {
    state.fansList = payload
  },
  ['SET_FANS_GROUP'] (state, payload) {
    state.fansGroup = payload
  },
  ['SET_MY_WEIBO'] (state, payload) {
    state.myWeibo = payload
  },
  ['SET_EMOJIS'] (state, payload) {
    if (payload) {
      state.emojis = payload
    }
  }
}
export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
